var datatable;// 定义一个全局的dataTable
var beginDateTemp;
var endDateTemp;
var failureTypeList;
var interfaceList;
var signalProList;
var interface_data;
$(function() {
	init();
	/* $('#endDate').datetimepicker({
		timeFormat : 'HH:mm:ss',
		stepHour : 1,
		minuteMin: 0,
		minuteMax: 0,
		secondMin : 0,
		secondMax : 0
	}); 
	$("#singleDate").datepicker(); */
	//联想下拉
//	$("#sel_interface").combobox({
//		select:function(event,ui){
//	    	var val=$("#sel_interface").val();
//			getSignalProcessList("sel_signal",val);
//		}
//	});
//	$("#sel_signal").combobox();
//	$( "#sel_signal" ).autocomplete({
//	    create: function( event, ui ) {
//	        // event 是当前事件对象
//	        alert(11);
//	        // ui对象是空的，只是为了和其他事件的参数签名保持一致
//	    }
//	});
//	getInterfaceList("sel_interface");
	$('input[name="dateType"]').change(function(){
		operateDiv();
	})
	
//	$("#beginDateDiv,#endDateDiv").hide();
	//接口下拉框change事件
//	$('#sel_interface').change(function(){
//		alert(4);
//		var val=$("#sel_interface").val();
//		getSignalProcessList("sel_signal",val);
//	});	
	
});
function init(){
	$("#interfaceDiv").jqxComboBox({promptText: "-------------请选择----------------",width: 230,height: 25});
    $("#signalDiv").jqxComboBox({promptText: "--------------请选择----------------",width: 230,height: 25});
    
    var interfacesource =
    {
        datatype: "json",
        datafields: [
            { name: 'value' },
            { name: 'label' }
        ],
        url: "../../kpi/interfaceFailure/getInterfaceList",
        async: false
    };
    interface_data=new Array() ; 
    var interfaceAdapter = new $.jqx.dataAdapter(interfacesource);

    $("#interfaceDiv").jqxComboBox(
    { 
    	source: interfaceAdapter, 
    	promptText: "--------------请选择----------------",
    	displayMember: "label", 
    	valueMember: "value", 
    	width: 230, 
    	height: 25,
    	autoComplete: true,
    	renderer: function (index, label, value) {
    		
            var item = interfaceAdapter.records[index];
            
            if (item != null) {

                var label = item.label ;
                interface_data[index]=item;
                return label;

            }
            	return "";


        }
    });
    $("#interfaceDiv").val(interface_data[0]);
    
    getSignalList(2);
    $("#signalDiv").val(signal_data[0]);
    $("#interfaceDiv").on('change', function (event) {
    	getSignalList($(this).val());
    	
    });
    
    var param_dateType=$("#param_dateType").val();	
    var param_paramDate=$("#param_paramDate").val();	
    var param_interfaceType=$("#param_interfaceType").val();	
    var param_signalType=$("#param_signalType").val();	
    if(""!=param_signalType && param_paramDate!="" && param_interfaceType!="" && param_signalType!=""){
    	$("input:radio[name='dateType'][value='"+param_dateType+"']").eq(0).attr("checked",'checked');
    	if(param_dateType=="custom"){
    		$("#beginDate").val(param_paramDate);
    		$("#endDate").val(param_paramDate);
    	}else{
    		$("#singleDate").val(param_paramDate);
    	}
//    	getInterfaceList("sel_interface");
//    	$('#sel_interface').combobox('setValues', param_interfaceType);
    	//$("#sel_interface").val(param_interfaceType);
//    	getSignalProcessList("sel_signal",param_interfaceType);
//    	$('#sel_signal').combobox('setValues', param_signalType);
    	//$("#sel_signal").val(param_signalType);
    	//$("#interfaceDiv").val(param_interfaceType);
    	//$("#signalDiv").val(param_signalType);
    	operateDiv();
    	getList();
    }
    
}
function operateDiv(){
	 var dateType=$('input[name="dateType"]:checked ').val();
	 if(dateType!="custom"){
		 $("#beginDateDiv,#endDateDiv").hide();
		 $("#singleDateDiv").show();
	 }else{
		 $("#singleDateDiv").hide();
		 $("#beginDateDiv,#endDateDiv").show();
	 }
	
}
function getList(){
	 var interfaceVal=$("#interfaceDiv").val();
	 var signalVal=$("#signalDiv").val();
	 if(interfaceVal==""){
			alert("请选择接口！");
			return;
		}
	 if(signalVal==""){
			alert("请选择信令过程！");
			return;
		}
//	 	if(interfaceVal!="2" || signalVal!="1"){
//	 		alert("目前只支持查询S1-MME attach");
//	 		return;
//	 	}
	 var dateVal=$('input[name="dateType"]:checked ').val();
	 var beginDate=$("#beginDate").val().trim();
	 var endDate=$("#endDate").val().trim();
	 var singleDate=$("#singleDate").val().trim();
	 if(dateVal=="custom"){
		 //自定义
			if(beginDate==""){
				alert("请选择开始时间！");
				return;
			}
			if(endDate==""){
				alert("请选择结束时间！");
				return;
			}
			
			var beginDate1 = new Date(beginDate.replace(/-/g,"/"));
			var endDate2 = new Date(endDate.replace(/-/g,"/"));
			if((beginDate1-endDate2)>0){
				alert("开始时间不能大于结束时间！");
				return;
			}
	 }else{
			if(singleDate==""){
				alert("请选择日期！");
				return;
			}
			beginDate=singleDate;
			endDate=singleDate;
	 }
	
		$("#table tbody tr").remove();
		if (datatable == undefined || datatable == "undefined" || datatable == null) {
		} else {
			datatable.fnDestroy();
		}
		datatable = $('#table').dataTable({
			"bPaginate" : false, // 翻页功能
			"bLengthChange" : false, // 改变每页显示数据数量
			"bFilter" : false, // 过滤功能
			"bSort" : true, // 排序功能
			"bInfo" : false,// 页脚信息
			"bAutoWidth" : true,// 自动宽度
			 "fnDrawCallback": function (oSettings) {
				 },
			 "fnCreatedRow": function( nRow, aData, iDataIndex ) {
//				 var currentPage=$(".current").text();
//				 alert("currentPage"+currentPage);
//				 if(null==currentPage || currentPage==undefined || currentPage=="undefined" || currentPage=="")
//					 currentPage=1;
//				 var pageSize=$("select[name=table_length]").val();
//				 var start=(currentPage-1)*pageSize+iDataIndex+1;
//				 alert(start);
				 
			    },	 
			// "sPaginationType": "full_numbers",//显示分页的样式 还有simple_numbers等
//			"sScrollX" : "97%",
			"bScrollCollapse" : true,
//			"sScrollX" : "100%",
			"sScrollXInner" : "110%",
			"iDisplayLength" : 50,
			"oLanguage" : {
				"sLengthMenu" : "每页显示 _MENU_条",
				"sZeroRecords" : "没有找到符合条件的数据",
				"sProcessing" : "&nbsp;&nbsp;&nbsp;&nbsp;查询中，请稍等...",
				"sInfo" : "当前第 _START_ - _END_ 条　共计 _TOTAL_ 条",
				"sInfoEmpty" : "没有记录",
				"sInfoFiltered" : "(从 _MAX_ 条记录中过滤)",
				"sSearch" : "搜索：",
				"oPaginate" : {
					"sFirst" : "首页",
					"sPrevious" : "前一页",
					"sNext" : "后一页",
					"sLast" : "尾页"
				}
			},
			"bProcessing" : true,
			"bServerSide" : true,
			"sAjaxSource" : "../../kpi/interfaceFailure/getList",
			"fnServerData" : function(sSource, aoData, fnCallback) {
				$.ajax({
					"type" : "post",
					"contentType" : "application/x-www-form-urlencoded",
					"url" : sSource,
					"dataType" : "json",
					"data" : {
						"aoData" : JSON.stringify(aoData),
						"dateType":dateVal,
						"interfaceType" : interfaceVal,
						"beginDate" : beginDate,
						"endDate" : endDate,
						"signalType" : signalVal
					}, // 以json格式传递
					"success" : function(resp) {
						beginDateTemp=resp.beginDate;
						endDateTemp=resp.endDate;
						failureTypeList=resp.failureTypeList;
						fnCallback(resp.allData);
					}
				});
			},
			columns : [ {
				data : "report_date"
			},
			{
				data : "interface"
			}, {
				data : "signal_process"
			}, {
				data : "failure_type"
			}, {
				data : "failure_type"
			}, {
				data : "failure_count",
				render : function(value,type,row){
					var report_date = row.report_date;
					var interfaceType = row['interface'];
					var signal_process = row.signal_process;
					var failure_type = row.failure_type;
					
					var	url = "<a href='#' onclick=\"failureCountDetail('"
								+ report_date + "','"
								+ interfaceType + "','"
								+ signal_process + "','"
								+ failure_type + "','"
								+"')\"  >"+ value + "</a>";
					return url;
				}
			}, {
				data : "failure_rate"
			}],
			aoColumnDefs: [
	       {	
	    	   
	    	   
	            "render": function(data, type, full)  
	            {
	            	var interfaceName='';
	            	for(var j=0;j<interface_data.length;j++){
	            		var tempValue=interface_data[j].value;
	            		if(tempValue==data){
	            			interfaceName=interface_data[j].label;
	            			break;
	            		}
	            	}
	            	return interfaceName;
	            },			
	            "targets":1
	       },
	       {	
	    	   
	    	   
	            "render": function(data, type, full)  
	            {
	            	var signalName='';
	            	for(var j=0;j<signal_data.length;j++){
	            		var tempValue=signal_data[j].value;
	            		if(tempValue==data){
	            			signalName=signal_data[j].label;
	            			break;
	            		}
	            	}
	            	return signalName;
	            },			
	            "targets":2
	       },
	       {	
	    	   
	    	   
	            "render": function(data, type, full)  
	            {
	            	var url ="<a href='#' onclick=\"failureTypeDetail('"+full['interface']+"','"+full.signal_process+"','"+full.failure_type+"')\"  >"+full.failure_type+"</a>";
	            	return url;
	            },			
	            "targets":3
	       },
	       {		                   
	            "render": function(data, type, full)  
	            {
	            	var failureType=full.failure_type;
	            	var failureTypeName='';
	            	for(var j=0;j<failureTypeList.length;j++){
	            		var tempValue=failureTypeList[j].value;
	            		if(tempValue==failureType){
	            			failureTypeName=failureTypeList[j].label;
	            			break;
	            		}
	            	}
	            	return failureTypeName;
	            },			
	            "targets":4
	       },
	       /*{		                   
	            "render": function(data, type, full)  
	            {
	            	var url = "";
	            	
	            	url ="<a href='#' onclick=\"failureCountDetail('"+full['interface']+"','"+full.signal_process+"','"+full.failure_type+"','"+full.begin_date+"','"+full.end_date+"','"+full.date_type+"')\"  >"+full.failure_count+"</a>";
	            	return url;
	            },			
	            "targets":5
	       }*/
	       ]

		});
 
	}
function test(){
	alert(0);
	$("#table thead").hide();
}
function failureCountDetail(report_date,interfaceType,signal_process,failureType){
//	var dateVal=$('input[name="dateType"]:checked ').val();
//	var beginDate;
//	var endDate;
//	alert(dateVal);
//	if(dateVal=="custom"){
//		beginDate=beginDateTemp;
//		endDate=endDateTemp;
//	}else{
//		beginDate=$("#beginDate").val().trim();
//		endDate=$("#endDate").val().trim();
//	}
//	var interfaceVal=$("#sel_interface").val();
//	var procedureType=$("#sel_signal").val();
//	param.put("dateType", request.getParameter("dateType"));
//	param.put("beginDate", request.getParameter("beginDate"));
//	param.put("endDate", request.getParameter("endDate"));
//	param.put("interfaceType", request.getParameter("interfaceType"));
//	param.put("procedureType", request.getParameter("procedureType"));
//	param.put("failure_type", request.getParameter("failure_type"));
//	var url="/kpi/failureDetail/index?"+"&dateType="+date_type.trim()+"&beginDate="+begin_date.trim()+"&endDate="+end_date.trim()+"&interfaceType="+interfaceType.trim()+"&procedureType="+signalType.trim()+"&failureType="+failureType.trim();
	var dateType = $('input[name="dateType"]:checked ').val();
	var url = "/kpi/failurecount/index?beginDate="+report_date+"&endDate="+report_date+"&interfaceType="+interfaceType+"&signal_process="+signal_process+"&failureType="+failureType+"&dateType="+dateType;
	linkTab("故障数据统计",url);
	//window.location.href=url;
}
function failureTypeDetail(interfaceType,signalType,failureType){
	var url="/kpi/expertDiagnosis/index?Product_type="+interfaceType.trim()+"&Error_code="+failureType.trim()+"&Type=1&Interface="+signalType;
//	window.location.href=url;
	linkTab("专家诊断",url);
}

/**
 * 获取接口下拉框
 * @param selectId
 */
function getInterfaceList(selectId){
	//第一步empty
	 $("#"+selectId).empty();
	var str="<option value=''>--请选择--</option>";
	 $.ajax({  
        async:false,//使用同步的Ajax请求  
         type: "POST",  
         dataType: "json",
         url: "../../kpi/interfaceFailure/getInterfaceList?",  
         data: {},  
         success: function(data){  
       	 interfaceList=data.list;
     		if(null!=interfaceList && undefined!=interfaceList && interfaceList!="undefined" && interfaceList!=""){
     			for(var m=0;m<interfaceList.length;m++){
     				var json=interfaceList[m];
     				str+="<option value="+json.value+">"+json.label+"</option>";
     			}
     		}
     		$("#"+selectId).append(str);
         },
         error:function(data){
       	  alert(data.msg);
       	  $("#"+selectId).append(str);
         }
     }); 
}
/**
 * 获取信令过程下拉框
 * @param selectId
 * @param id
 */
function getSignalProcessList(selectId,id){
	//第一步empty
	 $("#"+selectId).val("");
	 $("#"+selectId).empty();
	var str="<option value=''>--请选择--</option>";
	 $.ajax({  
         async:false,//使用同步的Ajax请求  
          type: "POST",  
          dataType: "json",
          url: "../../kpi/interfaceFailure/getSignalProcessList?",  
          data: {"id":id},  
          success: function(data){  
        	signalProList=data.list;
      		if(null!=signalProList && undefined!=signalProList && signalProList!="undefined" && signalProList!=""){
      			for(var m=0;m<signalProList.length;m++){
      				var json=signalProList[m];
      				str+="<option value="+json.value+">"+json.label+"</option>";
      			}
      		}
      		$("#"+selectId).append(str);
          },
          error:function(data){
        	  alert(data.msg);
        	  $("#"+selectId).append(str);
          }
      }); 
}


function getSignalList(id){
	var signalsource =
    {
        datatype: "json",
        datafields: [
            { name: 'label' },
            { name: 'value' }
        ],
        url: "../../kpi/interfaceFailure/getSignalProcessList?id="+id,
        async: false
    };
	var signalAdapter = new $.jqx.dataAdapter(signalsource);
	 signal_data=new Array() ; 
    // 信令类型名称
    $("#signalDiv").jqxComboBox(
    { 
    	source: signalAdapter, 
    	promptText: "--------------请选择----------------",
    	displayMember: "label", 
    	valueMember: "value", 
    	width: 230, 
    	height: 25,
    	autoComplete: true,
    	renderer: function (index, label, value) {
    		
            var item = signalAdapter.records[index];
            
            if (item != null) {

                var label = item.label ;
                signal_data[index]=item;
                return label;

            }
            	return "";


        }
    });
    $("#signalDiv").val("");
}